﻿using DataAccess;
using QuanLyKhachSan.Validation;
using System;
using System.Linq;


namespace QuanLyKhachSan
{
    public class QuyenManager:IValidator<Quyen>,ILenhThucThi<Quyen>
    {
        public ActionResult Them(Quyen obj)
        {
            var action = new ActionResult();

            using (var ql = new QLKhachSanContext())
            {
                var query = from q in ql.Quyens
                            where q.MaQuyen.Equals(obj.MaQuyen)
                            select q;

                try
                {
                    if (!query.Any())
                    {
                        ql.Quyens.Add(obj);
                        ql.SaveChanges();
                    }
                    else
                    {
                        action.Exceptions.Add(new Exception("Đã tồn tại quyền có mã " + obj.MaQuyen));
                    }
                }
                catch (Exception e)
                {
                    action.Exceptions.Add(e);
                }
            }

            return action;
        }

        public ActionResult Xoa(string ma)
        {
            var action = new ActionResult();

            using (var ql = new QLKhachSanContext())
            {
                var query = (from q in ql.Quyens
                             where q.MaQuyen == ma
                             select q).FirstOrDefault();
                try
                {
                    if (query == null)
                    {
                        action.Exceptions.Add(new Exception("Không tồn tại quyền có mã " + ma));
                    }
                    else
                    {
                        query.TrangThai = "Đã xóa";
                        ql.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    action.Exceptions.Add(e);
                }
            }

            return action;
        }

        public ActionResult Xoa(Quyen ma)
        {
            var action = new ActionResult();

            using (var ql = new QLKhachSanContext())
            {
                var query = (from q in ql.Quyens
                             where q.MaQuyen.Equals(ma.MaQuyen)
                             select q).FirstOrDefault();
                try
                {
                    if (query == null)
                    {
                        action.Exceptions.Add(new Exception("Không tồn tại quyền có mã " + ma));
                    }
                    else
                    {
                        query.TrangThai = "Đã xóa";
                        ql.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    action.Exceptions.Add(e);
                }
            }

            return action;
        }

        public ActionResult CapNhat(Quyen obj)
        {
            var action = new ActionResult();

            using (var ql = new QLKhachSanContext())
            {
                var query = (from q in ql.Quyens
                             where q.MaQuyen.Equals(obj.MaQuyen)
                             select q).FirstOrDefault();
                try
                {
                    if (query == null)
                        action.Exceptions.Add(new Exception("Không tồn tại quyền có mã " + obj.MaQuyen));
                    else
                    {
                        query.TenQuyen = obj.TenQuyen;
                        query.TrangThai = obj.TrangThai;
                        ql.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    action.Exceptions.Add(e);
                }
            }

            return action;
        }

        public ActionResultAsList<Quyen> LayDanhSach()
        {
            var action = new ActionResultAsList<Quyen>();

            var ql = new QLKhachSanContext();
            try
            {
                var query = from q in ql.Quyens
                            where q.TrangThai != "Đã xóa" || q.TrangThai == null
                            select q;
                action.Result = query.ToList();
                if (action.Result.Count == 0)
                    action.Exceptions.Add(new Exception("Dữ liệu quyền trống"));
            }
            catch (Exception e)
            {
                action.Exceptions.Add(e);
            }

            return action;
        }

        public ValidateResult Validate(Quyen obj)
        {
            var validate = new ValidateResult();

            if(string.IsNullOrEmpty(obj.TenQuyen))
                validate.Message.Add("Chưa có tên quyền");

            return validate;
        }
    }
}
